#pragma once

#include "json.hpp"
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <opencv2/highgui.hpp> //OpenCV终端部署
#include <opencv2/opencv.hpp>  //OpenCV终端部署

using namespace std;
using namespace cv;

#define COLSIMAGE 320    // 图像的列数
#define ROWSIMAGE 240    // 图像的行数
#define LABEL_BOMB 0      // AI标签：爆炸物
#define LABEL_BRIDGE 1    // AI标签：坡道
#define LABEL_SAFETY 2    // AI标签：普通车辆
#define LABEL_CONE 3      // AI标签：锥桶
#define LABEL_CROSSWALK 4 // AI标签：斑马线
#define LABEL_DANGER 5    // AI标签：危险车辆
#define LABEL_EVIL 6      // AI标签：恐怖分子
#define LABEL_BLOCK 7     // AI标签：障碍物
#define LABEL_PATIENT 8   // AI标签：伤员
#define LABEL_PROP 9      // AI标签：道具车
#define LABEL_SPY 10      // AI标签：嫌疑车辆
#define LABEL_THIEF 11    // AI标签：盗贼
#define LABEL_TUMBLE 12   // AI标签：跌倒

int speed = 10;
int commonspeed;
int bridgespeed;
int resuenspeed;
int dangerspeed;
double kp1 = 2.8;
double kp = kp1;
double kd1 = 0;
int showimg = 0;
int camera = 0;
int cutimgup = 20;
int cutimgdown = 20;
int saveimg = 0;
int servoline = 30;
bool buzzer = false;
int buzzernum = 10;
int usefilehave(void)
{
    ifstream file("../config/use.txt");
    if(!file)
    {
    cerr << "Error opening use.txt" << endl;  
    return 1;  
    }
    // 读取数据
    string line;
    while (getline(file, line)) {
        istringstream iss(line);
        string key;
        if (!(iss >> key)) {
            continue; // 忽略无效行
        }
        if (key == "kp1") {
            if (!(iss >> kp1)) {
                cerr << "无法读取kp1的值" << endl;
                return 1;
            }
            cerr << "获取的kp1:"  << kp1 << endl;
        } 
        else if (key == "kd1") {
            if (!(iss >> kd1)) {
                cerr << "无法读取kd1的值" << endl;
                return 1;
            }
            cerr << "获取的kd1:"  << kd1 << endl;
        }
        else if (key == "showimg") {
            if (!(iss >> showimg)) {
                cerr << "无法读取showimg的值" << endl;
                return 1;
            }
            cerr << "读取的showimg值" << showimg << endl;
        }
        else if (key == "camera") {
            if (!(iss >> camera)) {
                cerr << "无法读取camera的值" << endl;
                return 1;
            }
            cerr << "读取的camera值" << camera << endl;
        }
        else if (key == "commonspeed") {
            if (!(iss >> commonspeed)) {
                cerr << "无法读取commonspeed的值" << endl;
                return 1;
            }
            cerr << "读取的commonspeed值" << commonspeed << endl;
        }
        else if (key == "bridgespeed") {
            if (!(iss >> bridgespeed)) {
                cerr << "无法读取bridgespeed的值" << endl;
                return 1;
            }
            cerr << "读取的bridgespeed值" << bridgespeed << endl;
        }
        else if (key == "resuenspeed") {
            if (!(iss >> resuenspeed)) {
                cerr << "无法读取resuenspeed的值" << endl;
                return 1;
            }
            cerr << "读取的resuenspeed值" << resuenspeed << endl;
        }      
        else if (key == "dangerspeed") {
            if (!(iss >> dangerspeed)) {
                cerr << "无法读取dangerspeed的值" << endl;
                return 1;
            }
            cerr << "读取的dangerspeed值" << dangerspeed << endl;
        } 
        else if (key == "cutimgup") {
            if (!(iss >> cutimgup)) {
                cerr << "无法读取cutimgup的值" << endl;
                return 1;
            }
            cerr << "读取的cutimgup值" << cutimgup << endl;
        }  
        else if (key == "cutimgdown") {
            if (!(iss >> cutimgdown)) {
                cerr << "无法读取cutimgdown的值" << endl;
                return 1;
            }
            cerr << "读取的cutimgdown值" << cutimgdown << endl;
        } 
       else if (key == "saveimg") {
            if (!(iss >> saveimg)) {
                cerr << "无法读取saveimg的值" << endl;
                return 1;
            }
            cerr << "读取的saveimg值" << saveimg << endl;
        }  
       else if (key == "servoline") {
            if (!(iss >> servoline)) {
                cerr << "无法读取servoline的值" << endl;
                return 1;
            }
            cerr << "读取的servoline值" << servoline << endl;
        }   
       else if (key == "buzzernum") {
            if (!(iss >> buzzernum)) {
                cerr << "无法读取buzzernum的值" << endl;
                return 1;
            }
            cerr << "读取的buzzernum值" << buzzernum << endl;
        }   
        // 忽略其他key或添加更多key的处理
    }
    // 关闭文件
    file.close();
    return 0; 
}
/**
 * @brief 存储图像至本地
 *
 * @param image 需要存储的图像
 */
void savePicture(Mat &image)
{
    // 存图
    string name = ".jpg";
    static int counter = 0;
    counter++;
    string img_path = "../src/";
    name = img_path + to_string(counter) + ".jpg";
    imwrite(name, image);
}

string rgb_window_name = "RGB";
string gram_window_name = "gram";
void display_window_init() 
{
  namedWindow(rgb_window_name, WINDOW_NORMAL);
  resizeWindow(rgb_window_name, 640, 480);
  moveWindow(rgb_window_name, 0, 0);

  namedWindow(gram_window_name, WINDOW_NORMAL);
  resizeWindow(gram_window_name, 640, 480);
  moveWindow(gram_window_name, 640, 480);
}


// vector<Point> interpolatePoints(const Point& p1, const Point& p2, int n) {  //补线函数
//     vector<Point> points; 
//     // if (n <= 0 || p1 == p2) return points; 
//     double dx = static_cast<double>(p2.x - p1.x) / (n + 1);  
  
//     for (int i = 0; i <= n; ++i) {  
//         points.emplace_back(static_cast<int>(p1.x + i * dx), static_cast<int>(p1.y - i));  
//     }  
//     return points;  
// } 


